<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Hash;
use DB;
use Mail;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests\HomeLoginPostRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;


class BookstoreController extends Controller
{
    public function getBook(Request $request){
        // dd($request->all());
        //接收收索框传过来的值
        $num = $request->num;
        $add = $request->add;
        $val = $request->value;
        if(empty($num)){
            //查询所有书籍
            $book = DB::table('hs_books')
                    ->join('hs_member','hs_member.id','=','hs_books.aid')
                    ->select('hs_books.*','hs_member.name')
                    ->get();

            $books = DB::select('select id,title,max(time),time from hs_articles group by bid');
            $time = DB::select('select id,title,min(time),time from hs_articles group by bid');
            // dd($books);
            // dd($book);
        }else{
            if($add == 1){
                //根据收索框传过来的值进行综合数据收索
                $book = DB::table('hs_books')->where('hs_books.b_name','like','%'.$num.'%')
                        ->join('hs_member','hs_books.aid','=','hs_member.id')
                        ->select('hs_books.*','hs_member.name')
                        ->get();
                $books = DB::select('select title,max(time),time from hs_articles group by bid');
                $time = DB::select('select id,title,min(time),time from hs_articles group by bid');
                // if()  
            }else if($add == 2){
                //根据作品的名字收索
                $book = DB::table('hs_books')->where('hs_books.b_name','like','%'.$num.'%')
                        ->join('hs_member','hs_member.id','=','hs_books.aid')
                        ->select('hs_books.*','hs_member.name')
                        ->get();
                $books = DB::select('select title,max(time),time from hs_articles group by bid');
                $time = DB::select('select id,title,min(time),time from hs_articles group by bid');
            }else if($add == 3){
                //根据作者的名字收索
                $book = DB::table('hs_member')->where('hs_member.name','like','%'.$num.'%')
                        ->join('hs_member','hs_member.id','=','hs_books.aid')
                        ->select('hs_books.*','hs_member.name')
                        ->get();
                $books = DB::select('select title,max(time),time from hs_articles group by bid');
                $time = DB::select('select id,title,min(time),time from hs_articles group by bid');
            }else if ($add == 4) {
                //根据作品简介收索
                $book = DB::table('hs_books')->where('hs_books.b_desc','like','%'.$num.'%')
                        ->join('hs_member','hs_member.id','=','hs_books.aid')
                        ->select('hs_books.*','hs_member.name')
                        ->get();
                $books = DB::select('select title,max(time),time from hs_articles group by bid');
                $time = DB::select('select id,title,min(time),time from hs_articles group by bid');
            }else{
                //其它条件没有add参数的时候根据类型查询
                $book = DB::table('hs_books')->where('b_cate',$val)
                        ->join('hs_member','hs_member.id','=','hs_books.aid')
                        ->select('hs_books.*','hs_member.name')
                        ->get();
                $books = DB::select('select title,max(time),time from hs_articles group by bid');
                $time = DB::select('select id,title,min(time),time from hs_articles group by bid');
            }
        }
        // dd($book);
        //解析模板
        return view('home.detal.book',['book'=>$book,'books'=>$books,'time'=>$time]);
       
    }

    public function getBooks(Request $request){
        $cid = $request->input('cid');
        $isend = $request->input('isend');
           $res = DB::table('hs_books')
                ->join('hs_member','hs_member.id','=','hs_books.aid')
                ->join('hs_cates','hs_cates.cid','=','hs_books.b_cate')
                ->where('hs_books.b_cate','=',$cid)
                ->select('hs_books.b_name','hs_cates.cname','hs_books.isend','hs_books.b_desc','hs_books.b_pic','hs_member.name')->get();
           $res = json_encode($res);
           return $res;
        
    }

    public function join(Request $request)
    {
        // 查询该用户的id
        $id = session('id');
        //执行数据插入书架
        $book = DB::table('hs_bookrack')->insert(['aid'=>$id,'bo_id'=>$request->id,'z_id'=>$request->z_id]);

        //返回个人书架
        return redirect('/bookrack');
        
    }

    
}

